package com.wyj.livedata

import androidx.databinding.BaseObservable
import androidx.databinding.Observable
import androidx.lifecycle.MutableLiveData

class LiveData4Entity<T : BaseObservable> : MutableLiveData<T> {
    override fun setValue(value: T?) {
        getValue()?.removeOnPropertyChangedCallback(callback)
        value?.addOnPropertyChangedCallback(callback)
        super.setValue(value)
    }

    constructor() : super()
    constructor(value: T?) : super(value) {
        value?.addOnPropertyChangedCallback(callback)
    }

    val callback = object : Observable.OnPropertyChangedCallback() {
        override fun onPropertyChanged(sender: Observable?, propertyId: Int) {
            value = value
        }
    }
}